<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
cfc('core')->checklogin();
if($_RQ['op']=='get'){
	set_html('title','直播间');
	$id=intval($_RQ['id']);
	//来自其他插件的购物
	$this->session('fromplugin_name',$_RQ['plugin_name']);
	$this->session('frompid',intval($_RQ['frompid']));
	if($id){
		$this->session('id',$id);
	}
	if(!$id){
		exi('没有直播间ID','error','/index/');
	}
	$item=pdo_get('jy_weishop_live_room',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('选择的直播间不存在，请重新选择！','error','/index/');
	}
	if($item['type']!=2 && !$item['video']){
		$video_info = $this->model('model')->getLiveInfo($item['url'], $item['livept']);
		if (!is_error($video_info)) {
			$item['video']=$video_info['hls_url'];
			if($video_info['poster']){
				//pdo_update('jy_weishop_live_room', array('image' => $video_info['poster']), array('uniacid' => $_SESSION['uniacid'], 'id' => $item['id']));
			}
		}
	}
	$item['video']=str_replace('http:','', $item['video']);
	$item['video']=str_replace('https:','', $item['video']);
	pdo_update('jy_weishop_live_room', array('looks +=' => 1), array('uniacid' => $_SESSION['uniacid'], 'id' => $item['id']));
	$islookid=pdo_getcolumn('jy_weishop_live_looklog',array('uniacid' => $_SESSION['uniacid'], 'roomid' => $item['id'],'mid'=>$_SESSION['uid']),'id');
	if($islookid){
		pdo_update('jy_weishop_live_looklog', array('looks +=' => 1), array('uniacid' => $_SESSION['uniacid'], 'id' => $islookid));
	}else{
		$view_data = array('uniacid' => $_SESSION['uniacid'], 'mid' => $_SESSION['uid'], 'roomid' => $item['id']);
		$view_data['looks'] = 1;
		pdo_insert('jy_weishop_live_looklog', $view_data);
	}
	$item['islike']=pdo_getcolumn('jy_weishop_live_dy',array('roomid'=>$item['id'],'uniacid' => $_SESSION['uniacid'],'mid' => $_SESSION['uid']),'id');
	$item['islike']=$item['islike']?1:0;
	$item['isvote']=pdo_getcolumn('jy_weishop_live_likelog',array('roomid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$item['isvote']=$item['isvote']?1:0;
	$item['votes']=pdo_count('jy_weishop_live_likelog',array('roomid'=>$item['id'],'uniacid' => $_SESSION['uniacid']),'id');
	$fans=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
	$fans['time']=date('Y-m-d H:i:s',TIMESTAMP);
	$fans['nickname']=$fans['nickname']?$fans['nickname']:'匿名';
	$return['member']=array_merge($fans,$return['member']);
	$item['livetime2']=date('Y-m-d H:i:s',$item['livetime']);
	$item['nestable']=json_decode($item['nestable'],true);
	$item['menu']=json_decode($item['menu'],true);
	if (empty($item['nestable'])) {
		$item['nestable'] = array('interaction', 'goods', 'introduce');
	}
	if (empty($item['menu'])) {
		$item['menu'] = array(
			'interaction'=>array('name'=>'直播间'),
			'goods'=>array('name'=>'商城'),
			'introduce'=>array('name'=>'介绍'),
			'customname1'=>array('name'=>'自定义'),
			'customname2'=>array('name'=>'自定义'),
			'customname3'=>array('name'=>'自定义'),
			'customname4'=>array('name'=>'自定义'),
		);	
	}
	$item['goodsid']=json_decode($item['goodsid'],true);
	$item['image'] = $item['image'] ? tomedia($item['image']) : tomedia('../core/resource/images/nopic.jpg');
	$return['item']=$item;
	$return['goodslist']=array();
	foreach($item['goodsid'] as &$gv){		
		$goods=model('jy_weishop/goods')->get_goodsitem($gv['id']);
		$goods['liveprice']=$gv['price'];
		$return['goodslist'][]=$goods;
	}
	$pindex = max(1, 0);
	$psize = 50;
	$msgs=pdo_getslice('core_account_message',array('uniacid'=>$_SESSION['uniacid'],'aid'=>$item['id'],'plugin_name'=>'jy_h5live'),array($pindex,$psize),$total,'','','id asc');
	$msgslist=array();
	if($this->setting['room_desc']){
		$msgslist[]=array('uid'=>0,'nickname'=>$this->setting['room_desc']);
	}
	foreach($msgs as &$m){
		$m['avatar']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$m['uid']),'avatar');
		$m['nickname']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$m['uid']),'nickname');
		$msglastid=$m['id'];
		$msgslist[]=$m;
	}
	$return['item']['msglastid']=$msglastid;
	$return['msgs']=$msgslist;
	$liping=pdo_getall('jy_weishop_live_liping',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$return['liping']=$liping;
	$next=pdo_getslice('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id >'=>$item['id']),array($pindex,$psize),$total,'','','id asc');
	$return['nextid']=$next[0]['id'];
	if(!$return['nextid']){
		$next=pdo_getslice('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,2),$total,'','','id asc');
		$return['nextid']=$next[0]['id'];
	}
	$return['nexturl']=custom_url('jy_h5live/room//id='.$return['nextid'],true);
	$on=pdo_getslice('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id <'=>$item['id']),array($pindex,$psize),$total,'','','id desc');
	$return['onid']=$on[0]['id'];
	if(!$return['onid']){
		$on=pdo_getslice('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,2),$total,'','','id desc');
		$return['onid']=$on[0]['id'];
	}
	$return['onurl']=custom_url('jy_h5live/room//id='.$return['onid'],true);
	$return['wsConfig'] =array('isIos' => is_ios(),'isMobile'=>1);
	$return['tabname']='interaction';
	$return['shopset'] = plugin('jy_weishop')->plugin_setting('setting');
	$return['sharedata']=array(
		'title'=>$item['title']?$item['title']:'在线互动直播间',
		'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'在线互动直播间',
		'link'=>custom_url('jy_h5live/room//fromuser='.$_SESSION['uid'].'&id='.$item['id'],true),
		'imgUrl'=>$item['image']?tomedia($item['image']):tomedia($return['pluginset']['share_image'])
	);
	$return['account']=$_SESSION['account'];
	$getcoupon=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	foreach($getcoupon as $k=>&$cv){
			$cv=model('jy_weishop/coupon')->intcoupon($cv['id']);
			$cv['limittimes']=json_decode($cv['limittimes'],true);
			$cv['sendtime']=json_decode($cv['sendtime'],true);
			$cv['etime']=$cv['sendtime']['tend'];
			if($cv['sendtime']['tstart']>time()){
				$cv['issend']=0;
				$cv['etime']=$cv['sendtime']['tstart'];
			}elseif($cv['sendtime']['tstart']<time() && $cv['sendtime']['tend']>time()){
				$cv['issend']=1;
				$cv['etime']=$cv['sendtime']['tend'];
			}elseif($cv['sendtime']['tend']<time()){
				$cv['issend']=2;
			}
			$cv['sendtime']['tend2']=$cv['sendtime']['tend'];
			$cv['sendtime']['tstart2']=$cv['sendtime']['tstart'];
			$cv['sendtime']['tend']=date('Y-m-d',$cv['sendtime']['tend']);
			$cv['sendtime']['tstart']=date('Y-m-d',$cv['sendtime']['tstart']);
			if($cv['timelimittype']==2 && $cv['limittimes']['tend']<time()){
				unset($getcoupon[$k]);
			}else{
				$coupon[]=$cv;
			}	
	}
	$return['coupons']=$getcoupon;
	$return['redlist']=array();
	if($item['mid'] && $item['mid']==$_SESSION['uid']){
		$return['isboss']=1;
	}
	//POST提交：
	$post = array();
	$post['msg'] = $item['isscreen']?'不错，给你点赞！':'';
	$post['num'] = 1;
	$post['specid']=$selectspecs;
	$post['addressid']=$address['id']?:0;
	$post['optionsid']=$return['options']['id']?:0;
	$post['storeid']=0;
	$return['post']=$post;
	$return['pluginset']['red_fees']=$return['pluginset']['red_fees']?:array(
		array('fee'=>'10'),
		array('fee'=>'16'),
		array('fee'=>'18'),
		array('fee'=>'22'),
		array('fee'=>'28'),
		array('fee'=>'58'),
	);
	exi($return);
}

if($_RQ['op']=='addtocart'){
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	if(!$id){
		exi('选择的商品不存在，请重新选择！','error');
	}
	if(!$number){
		$number=1;
	}
	$spsids = '';
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
	}
	$optionid = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$ifgoods=pdo_get('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$goods['price'],
		'optionid'=>$optionid,
	);
	if($ifgoods){
		$data['total']=$ifgoods['total']+$number;
		if (0 < $goods['usermaxbuy']) {
			$order_goodscount = pdo_fetchcolumn('select ifnull(sum(og.total),0)  from ' . tablename('jy_weishop_order_goods') . ' og ' . ' left join ' . tablename('jy_weishop_order') . ' o on og.orderid=o.id ' . ' where og.goodsid=:goodsid and  o.status>=1 and o.mid=:mid  and og.uniacid=:uniacid ', array(':goodsid' => $goods['id'], ':uniacid' => $_SESSION['uniacid'], ':mid' => $_SESSION['uid']));
			if ($goods['usermaxbuy'] <= $order_goodscount +$ifgoods['total']) {
				exi('不能再加了，超过该商品最大购买量！','error');
			}
		}
		if (0 < $goods['maxbuy']) {
			if ($goods['maxbuy'] <= $data['total']) {
				exi('不能再加了，超过该商品单次购买量！','error');
			}
		}
		pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
	}
	else{
		$data['total']=$number;
		//如果来自其他商户分销（保存商户ID）
		$data['total']['merchid']=$this->session('frommerchid');
		pdo_insert('jy_weishop_member_cart',$data);
	}
	exi('成功加入购物车！');
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$post['goodsid']?:$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$return['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']));
	//默认商品规格价格：
	if($return['options']){
		$goods['price']=$return['options']['price'];
	}
	$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	$return['carttotal']=$return['carttotal']?:0;
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='change_address'){
	$post=get_postdata();
	$addressid = intval($post['addressid']);
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$addressid));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['address'] =$address;
	exi($return);
}
if($_RQ['op']=='delmsg'){
	$postdata=get_postdata();
	$roomid = $this->session('id');
	pdo_delete('core_account_message',array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
	$socketdata=array('type'=>'delmsg','id'=>$postdata['id']);
	//cfc('jinyun_socket')->send2room($socketdata,'room'.$roomid);
	exi('撤销成功！');
}
if($_RQ['op']=='setmsg'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$postdata=get_postdata();
	$roomid = $this->session('id');
	$data=array(
		'aid'=>$roomid,
		'content'=>trim($postdata['msg']),
		'username'=>$member['nickname']?$member['nickname']:'匿名游客',
		'uid'=>$_SESSION['uid'],
		'uniacid'=>$_SESSION['uniacid'],
		'plugin_name'=>'jy_h5live',
		'status'=>1,
		'createtime'=>time(),
	);
	pdo_insert('core_account_message',$data);
	$data['id']=pdo_insertid();
	$data['nickname']=$data['username'];
	$return=array('msg'=>'评论成功！','avatar'=>$member['avatar'],'data'=>$data);
	//$socketdata=array('type'=>'msg','data'=>$data);
	//cfc('jinyun_socket')->send2room($socketdata,'room'.$data['aid']);
	exi($return);
}
if($_RQ['op']=='like'){
	if($_RQ['id']){
		$id=$_RQ['id'];
	}else{	
		$id = $this->session('id');
	}
	if(!$id){
		exi('没有找到该直播间，请重新选择！','error');
	}
	$like=pdo_get('jy_weishop_live_dy',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'roomid'=>$id),'id');
	if($like){
			pdo_delete('jy_weishop_live_dy',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'roomid'=>$id));
			exi('取消成功');
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'roomid'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_live_dy',$insert);
		exi('关注成功');
	}	
}
if($_RQ['op']=='vote'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该直播，请重新选择！','error');
	}
	$like=pdo_get('jy_weishop_live_likelog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'roomid'=>$id),'id');
	if($like){
			exi(array('isvote'=>1,'success'=>1,'msg'=>'你已点赞'));
			pdo_delete('jy_weishop_live_likelog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'roomid'=>$id));
			exi(array('success'=>1,'msg'=>'取消成功'));
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'roomid'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_live_likelog',$insert);
		$istanchu=0;
		$room=pdo_get('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($return['pluginset']['nosendred']!=1 && $room['islikered']==1){
			$istanchu=1;
		}
		exi(array('istanchu'=>$istanchu,'success'=>1,'msg'=>'点赞成功'));
	}	
}
if($_RQ['op']=='check_buy'){
	model('jy_weishop/goods')->check_buy($_RQ['goodsid'],$_RQ['num'],$_RQ['optionsid']);
	exi('','','jy_weishop/order.create//goodsid='.$_RQ['goodsid'].'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid']);
}
if($_RQ['op']=='getcomments'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	$return['list']=model('jy_weishop/goods')->get_comments($goodsid,6,$comment_total,$_RQ['type'],$_RQ['page']);
	exi($return);
}
if($_RQ['op']=='sendhb'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取直播id','error');
	}
	if($return['pluginset']['nosendred']==1){
			exi('系统不支持发红包！','error');
	}
	$post=array();
	$post['money']=0;
	$opus=pdo_get('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($_RQ['postdata']){
	   $postdata=get_postdata();
	   $money=$postdata['money'];
	   $username=$postdata['username'];
		if($money<1){
		   exi('红包金额需大于等于1元','error');
	   }
	   if($money>200){
		   exi('单个红包金额最多不超过200元','error');
	   }
	   if(!$username){
		  // exi('请输入发送人姓名！','error');
	   }
	   //创建红包订单
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'roomid'=>$id,
			'omid'=>$opus['mid'],
			'fee'=>$money,
			'ordersn'=>get_ordersn('jy_weishop_live_redlog','ordersn'),
			'username'=>$username,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_weishop_live_redlog',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$money;
	    $params['title']='发送红包订单';
		$params['id']=$id;
		$params['plugin']='jy_h5live';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_h5live/room/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_weishop_live_redlog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！发送红包订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！发送红包订单金额不匹配！','error');
	}
	pdo_update('jy_weishop_live_redlog',array('ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//发红包
	$params =array(
			'money'=>$payresult['fee'],
			'mid'=>$order['omid'],
			'total_num'=>1,
			'remark'=>'抽红包-'.$item['title'],
	);
	$get_money=cfc('red_pack')->get_money($params,1);
 	if($get_money){
		pdo_update('jy_weishop_live_redlog',array('status'=>1),array('id'=>$order['id']));
		if($_SESSION['uid']!=$order['omid']){
			//发送消息
			$newmember = pdo_get('core_members',array('id'=>$_SESSION['uid']),'nickname');
			$msg =array('username'=>$newmember['nickname']);
			//set_sms($newmember['mobile'],$msg,1,$_SESSION['uid']);
			$basic['getredtext']= $basic['getredtext']?$basic['getredtext']:'您收到一个粉丝的鼓励红包';
			//set_websms($basic['getredtext'],0,$order['omid'],'');
			//发送wokerman
			//cfc('jinyun_socket')->send2uid($basic['getredtext'],$order['omid']);//发送给指定用户
			$data=array(
				'aid'=>$order['roomid'],
				'content'=>'收到粉丝的鼓励红包《'.$order['fee'].'》元！',
				'username'=>pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname'),
				'uid'=>$_SESSION['uid'],
				'uniacid'=>$_SESSION['uniacid'],
				'plugin_name'=>'jy_h5live',
				'status'=>1,
				'createtime'=>time(),
			);
			$socketdata=array('type'=>'mygetred','data'=>$data);
			//cfc('jinyun_socket')->send2uid($socketdata,$order['omid']);
		}else{
			$data=array(
				'aid'=>$order['roomid'],
				'content'=>'主播发《'.$order['fee'].'》元红包,大家快抢啊！',
				'username'=>'主播',
				'uid'=>$_SESSION['uid'],
				'uniacid'=>$_SESSION['uniacid'],
				'plugin_name'=>'jy_h5live',
				'pageaction'=>'sendred',
				'status'=>1,
				'createtime'=>time(),
			);
			pdo_insert('core_account_message',$data);
			$socketdata=array('type'=>'sendred','data'=>$data);
			//cfc('jinyun_socket')->send2room($socketdata,'room'.$order['roomid']);
		}
		//发送模板消息
		$this->notice('setred',$_SESSION['uid'],$order['o_id'],$order['id']);
		exi('红包转发成功','success');
	}
}

if($_RQ['op']=='chouhb'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取直播间','error');
	}
	$redid=intval($_RQ['redid']);
	$room=pdo_get('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($return['pluginset']['nosendred']==1){
			exi('系统不支持发红包！','error');
	}
	if($room['islikered']!=1){
			exi('该直播间不支持抽红包！','error');
	}
	if($redid){
		$dayhbnum=pdo_count('jy_weishop_live_gethblog',array('uniacid'=>$_SESSION['uniacid'],'roomid'=>$id,'redid'=>$redid,'status'=>1),'id');	
		if($dayhbnum){
			exi('你已抽红包','error');
		}
		$ishb=1;
		$fee=pdo_getcolumn('jy_weishop_live_redlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$redid,'status'=>1),'fee');
		$total_num=16;
	}else{
		$dayhbnum=pdo_count('jy_weishop_live_gethblog',array('uniacid'=>$_SESSION['uniacid'],'roomid'=>$id,'status'=>1),'id');	
		if($dayhbnum){
			//exi('你已抽红包','error');
		}
		//每日红包数量
		$allhbnum=pdo_getsum('jy_weishop_live_gethblog',array('uniacid'=>$_SESSION['uniacid'],'roomid'=>$id,'status'=>1),'fee');
		if($allhbnum>=$room['allred']){
			exi('红包已经抽完！下次活动再来吧！','error');
		}
		//生成一个随机数，判断是否会获得红包
		$rand=rand(1,100) ;
		if($rand>$room['hb_rate']){
			$ishb=2;
		}else{
			$ishb=1;
		}
		$fee=rand($room['minhb']*100,$room['maxhb']*100)/100;//金额
		$total_num=1;
	}
	$ok=0;
	//得红包
	if($ishb==1){
		$ok=1;
		if($return['pluginset']['ismoneyred']==1){
			$params=array(
				'money'=>$fee,
				'remark'=>'直播红包-'.$room['title'],
			);
			$res = model('core/member')->payred_out($_SESSION['uid'],$params);
		}else{
			$params =array(
				'money'=>$fee,
				'mid'=>$_SESSION['uid'],
				'total_num'=>$total_num,
				'remark'=>'直播红包-'.$room['title'],
			);	 
			$get_money=cfc('red_pack')->get_money($params,1);
		}		
		if($res['result_code'] == 'SUCCESS' || $get_money>0){
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'roomid'=>$room['id'],
				'omid'=>$room['mid'],
				'status'=>1,
				'fee'=>$fee,
				'redid'=>$redid,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_weishop_live_gethblog',$data);
			$return['fee']=$fee;
			$return['ok']=$ok;
			$data=array(
				'aid'=>$room['id'],
				'content'=>'抢到《'.$fee.'》元红包',
				'username'=>pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname'),
				'uid'=>$_SESSION['uid'],
				'uniacid'=>$_SESSION['uniacid'],
				'plugin_name'=>'jy_h5live',
				'pageaction'=>'getred',
				'status'=>1,
				'createtime'=>time(),
			);
			pdo_insert('core_account_message',$data);
			$socketdata=array('type'=>'getred','data'=>$data);
			//cfc('jinyun_socket')->send2room($socketdata,'room'.$room['id']);
		}else{
			$return['msg']='红包发送失败';
		}
	}else{
		$return['msg']='很遗憾没能抽到红包';
	}
	if($redid){
		$return['redlist']=pdo_getall('jy_weishop_live_redlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$redid,'status'=>1));
		foreach($return['redlist'] as &$m){
			$m['avatar']=pdo_getcolumn('core_members',array('id'=>$m['mid']),'avatar');
			$m['nickname']=pdo_getcolumn('core_members',array('id'=>$m['mid']),'nickname');
		}
	}
	exi($return);	
}
if($_RQ['op']=='sendlp'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取直播id','error');
	}
	if($return['pluginset']['nosendliping']==1){
			exi('系统不支持发红包！','error');
	}
	$postdata=get_postdata();
	$lpid=$postdata['lpid'];
	if(!$lpid){
		exi('未获取到礼品信息','error');
	}
	//exi($lpid,'error');
	$opus=pdo_get('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	//查询礼品
	$liping=pdo_get('jy_weishop_live_liping',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lpid));
	//礼品订单
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'roomid'=>$id,
			'lpid'=>$lpid,
			'fee'=>$liping['price'],
			'ordersn'=>get_ordersn('jy_weishop_live_sendlplog','ordersn'),
			'jifen'=>$liping['jifen'],
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_weishop_live_sendlplog',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$liping['price'];
	    $params['title']='赠送礼品订单';
		$params['id']=$id;
		$params['plugin']='jy_h5live';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='hdfk';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_h5live/room/lppayresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
		exi($return);

}
if($_RQ['op']=='lppayresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_weishop_live_sendlplog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！赠送礼品订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！赠送礼品订单金额不匹配！','error');
	}
	//活动
	pdo_update('jy_weishop_live_sendlplog',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
	$data=array(
		'aid'=>$order['roomid'],
		'content'=>'赠送礼品:《'.$order['title'].'》',
		'username'=>pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname'),
		'uid'=>$_SESSION['uid'],
		'uniacid'=>$_SESSION['uniacid'],
		'plugin_name'=>'jy_h5live',
		'pageaction'=>'liping',
		'status'=>1,
		'createtime'=>time(),
	);
	pdo_insert('core_account_message',$data);
	$socketdata=array('type'=>'liping','data'=>$data);
	//cfc('jinyun_socket')->send2room($socketdata,'room'.$order['roomid']);
	exi('赠送成功');
}
if($_RQ['op']=='sendcoupon'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取直播间','error');
	}
	$room=pdo_get('jy_weishop_live_room',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($return['pluginset']['nosendcoupon']==1){
			exi('系统不支持发优惠券！','error');
	}
	$coupons=str_array($return['pluginset']['couponids']);
	$coupon=model('jy_weishop/coupon')->intcoupon($coupons[0]);
	$data=array(
				'aid'=>$room['id'],
				'content'=>'主播发《优惠券》拉,别让券飞走了！',
				'username'=>'主播',
				'uid'=>$_SESSION['uid'],
				'uniacid'=>$_SESSION['uniacid'],
				'plugin_name'=>'jy_h5live',
				'pageaction'=>'sendcoupon',
				'status'=>1,
				'createtime'=>time(),
	);
	pdo_insert('core_account_message',$data);
	$socketdata=array('type'=>'sendcoupon','data'=>$data,'coupon'=>$coupon);
	//cfc('jinyun_socket')->send2room($socketdata,'room'.$room['id']);
	exi('发优惠券成功');
}
if($_RQ['op']=='getonemsg'){
	$aid = trim($_RQ['aid']);
	$lastid = intval($_RQ['lastid']);
	$log = pdo_get('core_account_message',array('id >'=>$lastid,'uniacid'=>$_SESSION['uniacid'],'aid'=>$aid,'plugin_name'=>'jy_h5live'));
	if($log){
		$log['avatar']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['uid']),'avatar');
		$log['nickname']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['uid']),'nickname');
		$res=array('log'=>$log);
	}else{
		$res=array('log'=>false);
	}
	exi($res);
}